'use strict';

const Controller = require('egg').Controller;

class StayMsgController extends Controller {
  /**
   *
   * @api {POST} /api/staymsg/create 创建留言
   * @apiName 创建留言
   * @apiGroup 留言模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {String} content 留言内容
   *
   *
   * @apiParamExample  {type} Request-Example:
    {
        "content":"我是留言"
    }
   *
   *
   * @apiSuccessExample {type} Success-Response:
    {
        "code": 200,
        "msg": "成功操作",
        "data": {
            "id": 1,
            "content": "我是留言",
            "projectId": 62,
            "updatedAt": "2022-01-19T11:17:38.201Z",
            "createdAt": "2022-01-19T11:17:38.201Z"
        }
    }
   *
   *
   */
  async create() {
    const { ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'content').checkDataRes;
    // 如果有缺少参数
    if (checkRes) {
      return ctx.helper.lackModule(checkRes);
    }
    try {
      const res = await service.web.stayMsg.create();
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
   *
   * @api {GET} /api/staymsg/get?limit=10&offset=0 获取留言
   * @apiName 获取留言
   * @apiGroup 留言模块
   * @apiVersion  1.0.0
   *
   *
   * @apiParam  {Number} [limit] 留言返回数
   * @apiParam  {Number} [offset] 留言偏移数
   *
   *
   *
   * @apiSuccessExample {type} Success-Response:
    {
        "code": 200,
        "msg": "成功操作",
        "data": {
            "count": 1,   //留言总数
            "rows": [
                {
                    "id": 1,  //留言id
                    "content": "我是留言",  //留言内容
                    "projectId": 62,    //项目id
                    "createdAt": "2022-01-19T11:17:38.000Z",
                    "updatedAt": "2022-01-19T11:17:38.000Z",
                    "Project": {
                        "id": 62,
                        "name": "haha", //项目名称
                        "createdAt": "2022-01-04T02:55:07.000Z",
                        "updatedAt": "2022-01-05T02:57:44.000Z"
                    }
                }
            ]
        }
    }
   *
   *
   */
  async get() {
    const { app, ctx, service } = this;
    try {
      const res = await service.web.stayMsg.get();
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }
}
module.exports = StayMsgController;
